package org.jeecg.modules.basesource.handler;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.basesource.entity.SbSbxx;
import org.jeecg.modules.basesource.entity.SbSyncRecord;
import org.jeecg.modules.basesource.service.ISbSbxxService;
import org.jeecg.modules.basesource.service.ISbSyncRecordService;
import org.jeecg.modules.basesource.service.IZsYjsfService;

import org.jeecg.modules.dzswj.GuangdongSfjrApiService;
import org.jeecg.modules.dzswj.vo.LoginSessionVo;
import org.jeecg.modules.dzswj.vo.request.CxnsrysbjlReqVo;
import org.jeecg.modules.dzswj.vo.response.CxnsrysbjlResponseVo;
import org.jeecg.modules.fk.service.IDjOrgService;
import org.jeecg.modules.qyxx.entity.CjQyxxStatus;
import org.jeecg.modules.qyxx.entity.DjNsrxxPO;
import org.jeecg.modules.qyxx.service.ICjQyxxStatusService;
import org.jeecg.modules.qyxx.service.IDjNsrxxService;
import org.jeecg.modules.qyxx.util.DateUtils;
import org.jeecg.modules.qyxx.util.RedisUtil;
import org.jeecg.modules.system.service.ISysDepartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.*;

@Slf4j
@Component
public class SbxxCjHandler {
    @Autowired
    private GuangdongSfjrApiService guangdongSfjrApiService;
    @Autowired
    private ISbSbxxService sbSbxxService;
    @Autowired
    private IDjNsrxxService nsrxxService;
    /*@Autowired
    private IDjOrgService djOrgService;
    @Autowired
    private IZsYjsfService zsYjsfService;*/
    @Autowired
    private GuangdongSfjrApiService sfjrApiService;
    /*@Autowired
    private ISysDepartService sysDepartService;*/
    @Autowired
    private ICjQyxxStatusService cjQyxxStatusService;
    @Autowired
    private ISbSyncRecordService sbSyncRecordService;
    /*@Autowired
    private IJhZzjgService jhZzjgService;*/

    private static final String CJ_SB_STATUS_KEY = "cjSbStatus";

    private static ExecutorService executor = new ThreadPoolExecutor(1, 1, 0L,
            TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(10000),
            new ThreadPoolExecutor.CallerRunsPolicy());


    // 采集申报信息数据(按季度)
    @XxlJob(value = "cjJdSbxxJob")
    public ReturnT<String> cjSbxxJob(String s) {
        List<Future> futureList = new ArrayList<>();
        DateUtils utils = DateUtils.getInstance();
        LambdaQueryWrapper<CjQyxxStatus> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CjQyxxStatus::getType, "03").eq(CjQyxxStatus::getStatus, "0");
        List<CjQyxxStatus> list = cjQyxxStatusService.list(queryWrapper);

        for (CjQyxxStatus cjQyxxStatus : list) {
            futureList.add(executor.submit(new Callable() {
                boolean flag = true;

                @Override
                public Object call() throws Exception {
                    log.info("开始同步企业{}，开始时间：{}， 结束时间: {}", cjQyxxStatus.getDjxh(),
                            DateUtils.getInstance().format(cjQyxxStatus.getKssj()),
                            DateUtils.getInstance().format(cjQyxxStatus.getJssj()));
                    Date kssj = cjQyxxStatus.getKssj();
                    Date jssj = cjQyxxStatus.getJssj();
                    //判断起止时间超过1年，则全量，否则是增量
                    Calendar kssjCal = Calendar.getInstance();
                    kssjCal.setTime(kssj);
                    Calendar jssjCal = Calendar.getInstance();
                    jssjCal.setTime(jssj);
                    int months = utils.getCountBeforeTwoTime(kssj, jssj);
                    // 全量
                    if (months >= 12) {
                        Date endTime = utils.getCurrentQuarterStartTime(jssj);
                        Calendar endCalendar = Calendar.getInstance();
                        endCalendar.setTime(endTime);
                        while (kssjCal.before(endCalendar)) {
                            String ssqq = utils.format(utils.getCurrentQuarterStartTime(kssjCal.getTime()));
                            String ssqz = utils.format(utils.getCurrentQuarterEndTime(kssjCal.getTime()));
                            DjNsrxxPO po = nsrxxService.getByNsrsbhOrShxydm(cjQyxxStatus.getNsrsbh());
                            SbSyncRecord sbSyncRecord = sbSyncRecordService.getByDjxhSsq(po.getDjxh(), ssqq, ssqz);
                            CxnsrysbjlReqVo reqVo = new CxnsrysbjlReqVo();
                            reqVo.setDjxh(po.getDjxh());
                            reqVo.setSkssqq(ssqq);
                            reqVo.setSkssqz(ssqz);
                            LoginSessionVo loginInfo = sfjrApiService.getNsrLoginSessionInfo(po.getNsrsbh());
                            if (sbSyncRecord == null) {
                                sbSyncRecord = new SbSyncRecord();
                                BeanUtils.copyProperties(reqVo, sbSyncRecord);
                                sbSyncRecord.setCreateTime(new Date());
                                sbSyncRecord.setType("206");
                                sbSyncRecord.setNsrsbh(po.getNsrsbh());
                            } else if ("200".equals(sbSyncRecord.getStatus()) && "206".equals(sbSyncRecord.getType())) {
                                kssjCal.add(Calendar.MONTH, 3);
                                continue;
                            }
                            sbSyncRecord.setUpdateTime(new Date());
                            try {
                                CxnsrysbjlResponseVo vo = guangdongSfjrApiService.cxnsrysbjl(loginInfo.getSessionId(), loginInfo.getZgswjdm(), reqVo);
                                if (vo == null) {
                                    sbSyncRecord.setStatus("200");
                                } else {
                                    List<CxnsrysbjlResponseVo.CwgzReturnVo> cwgzReturnVO = vo.getCwgzReturnVO();
                                    for (CxnsrysbjlResponseVo.CwgzReturnVo returnVo : cwgzReturnVO) {
                                        String skssqq = returnVo.getSkssqq();
                                        String skssqz = returnVo.getSkssqz();
                                        String sbrq = returnVo.getSbrq();
                                        SbSbxx sbxx = new SbSbxx();
                                        sbxx.setTynsrsbh(po.getTynsrsbh());
                                        sbxx.setSbxxuuid(returnVo.getSbuuid());
                                        sbxx.setSbuuid(returnVo.getSbuuid());
                                        sbxx.setDjxh(po.getDjxh());
                                        sbxx.setPzxh(StringUtils.hasLength(returnVo.getPzxh()) ? returnVo.getPzxh() : "");
                                        sbxx.setYzpzzlDm(StringUtils.hasLength(returnVo.getYzpzzlDm()) ? returnVo.getYzpzzlDm() : "");
                                        String sbfsDm = returnVo.getSbfsDm();
                                        if (StringUtils.hasLength(sbfsDm)) {
                                            sbxx.setSbfsDm(sbfsDm);
                                        } else {
                                            sbxx.setSbfsDm("");
                                        }
                                        if (StringUtils.hasLength(skssqq)) {
                                            sbxx.setNssbrq(utils.strToDate(sbrq, "yyyy-MM-dd"));
                                        }
                                        if (StringUtils.hasLength(skssqq)) {
                                            sbxx.setSkssqq(utils.strToDate(skssqq, "yyyy-MM-dd"));
                                        }
                                        if (StringUtils.hasLength(skssqz)) {
                                            sbxx.setSkssqz(utils.strToDate(skssqz, "yyyy-MM-dd"));
                                        }
                                        sbxx.setYbtse(returnVo.getYbtse().doubleValue());
                                        String zsxmDm = returnVo.getZsxmDm();
                                        if (StringUtils.hasLength(zsxmDm)) {
                                            sbxx.setZsxmDm(returnVo.getZsxmDm());
                                        } else {
                                            sbxx.setZsxmDm("");
                                        }
                                        sbxx.setCreateTime(new Date());
                                        sbxx.setUpdateTime(new Date());
                                        sbSbxxService.saveOrUpdateByMultiId(sbxx);
                                    }
                                    sbSyncRecord.setStatus("100");
                                    sbSyncRecord.setRemark("");
                                }
                            } catch (Exception e) {
                                log.error("调用申报接口错误,", e);
                                sbSyncRecord.setStatus("1");
                                sbSyncRecord.setRemark(e.getMessage() + ", 入参:" + JSON.toJSONString(reqVo));
                                flag = false;
                            }
                            sbSyncRecordService.saveOrUpdate(sbSyncRecord);
                            //加3个月
                            kssjCal.add(Calendar.MONTH, 3);
                        }
                    } else {
                        //增量数据要计算要同步哪个时间段。
                        //4,7,10,1月份分别同步上季度数据，其他月份不同步。
                        int month = jssjCal.get(Calendar.MONTH) + 1;
                        List<Integer> syncMonth = Arrays.asList(1, 4, 7, 10);
                        if (!syncMonth.contains(month)) {
                            cjQyxxStatus.setStatus("1");
                        } else {
                            jssjCal.add(Calendar.MONTH, -1);
                            String ssqq = utils.format(utils.getCurrentQuarterStartTime(jssjCal.getTime()));
                            String ssqz = utils.format(utils.getCurrentQuarterEndTime(jssjCal.getTime()));
                            CxnsrysbjlReqVo reqVo = new CxnsrysbjlReqVo();
                            DjNsrxxPO po = nsrxxService.getByNsrsbhOrShxydm(cjQyxxStatus.getNsrsbh());
                            SbSyncRecord sbSyncRecord = sbSyncRecordService.getByDjxhSsq(po.getDjxh(), ssqq, ssqz);
                            reqVo.setDjxh(po.getDjxh());
                            reqVo.setSkssqq(ssqq);
                            reqVo.setSkssqz(ssqz);
                            LoginSessionVo loginInfo = sfjrApiService.getNsrLoginSessionInfo(po.getNsrsbh());
                            if (sbSyncRecord == null) {
                                sbSyncRecord = new SbSyncRecord();
                                BeanUtils.copyProperties(reqVo, sbSyncRecord);
                                sbSyncRecord.setCreateTime(new Date());
                                sbSyncRecord.setType("206");
                                sbSyncRecord.setNsrsbh(po.getNsrsbh());
                            }
                            sbSyncRecord.setUpdateTime(new Date());
                            try {
                                CxnsrysbjlResponseVo vo = guangdongSfjrApiService.cxnsrysbjl(loginInfo.getSessionId(), loginInfo.getZgswjdm(), reqVo);
                                if (vo == null) {
                                    sbSyncRecord.setStatus("200");
                                } else {
                                    List<CxnsrysbjlResponseVo.CwgzReturnVo> cwgzReturnVO = vo.getCwgzReturnVO();
                                    for (CxnsrysbjlResponseVo.CwgzReturnVo returnVo : cwgzReturnVO) {
                                        String skssqq = returnVo.getSkssqq();
                                        String skssqz = returnVo.getSkssqz();
                                        String sbrq = returnVo.getSbrq();
                                        SbSbxx sbxx = new SbSbxx();
                                        sbxx.setTynsrsbh(po.getTynsrsbh());
                                        sbxx.setSbxxuuid(returnVo.getSbuuid());
                                        sbxx.setSbuuid(returnVo.getSbuuid());
                                        sbxx.setDjxh(po.getDjxh());
                                        sbxx.setPzxh(StringUtils.hasLength(returnVo.getPzxh()) ? returnVo.getPzxh() : "");
                                        sbxx.setYzpzzlDm(StringUtils.hasLength(returnVo.getYzpzzlDm()) ? returnVo.getYzpzzlDm() : "");
                                        String sbfsDm = returnVo.getSbfsDm();
                                        if (StringUtils.hasLength(sbfsDm)) {
                                            sbxx.setSbfsDm(sbfsDm);
                                        } else {
                                            sbxx.setSbfsDm("");
                                        }
                                        if (StringUtils.hasLength(skssqq)) {
                                            sbxx.setNssbrq(utils.strToDate(sbrq, "yyyy-MM-dd"));
                                        }
                                        if (StringUtils.hasLength(skssqq)) {
                                            sbxx.setSkssqq(utils.strToDate(skssqq, "yyyy-MM-dd"));
                                        }
                                        if (StringUtils.hasLength(skssqz)) {
                                            sbxx.setSkssqz(utils.strToDate(skssqz, "yyyy-MM-dd"));
                                        }
                                        sbxx.setYbtse(returnVo.getYbtse().doubleValue());
                                        String zsxmDm = returnVo.getZsxmDm();
                                        if (StringUtils.hasLength(zsxmDm)) {
                                            sbxx.setZsxmDm(returnVo.getZsxmDm());
                                        } else {
                                            sbxx.setZsxmDm("");
                                        }
                                        sbxx.setCreateTime(new Date());
                                        sbxx.setUpdateTime(new Date());
                                        sbSbxxService.saveOrUpdateByMultiId(sbxx);
                                    }
                                    sbSyncRecord.setStatus("100");
                                    sbSyncRecord.setRemark("");
                                }
                            } catch (Exception e) {
                                log.error("调用申报接口错误,", e);
                                sbSyncRecord.setStatus("1");
                                sbSyncRecord.setRemark(e.getMessage() + ", 入参:" + JSON.toJSONString(reqVo));
                                flag = false;
                            }
                            sbSyncRecordService.saveOrUpdate(sbSyncRecord);
                        }
                    }
                    if (flag) {
                        boolean b = RedisUtil.hasKey(CJ_SB_STATUS_KEY + "JD" + cjQyxxStatus.getNsrsbh() + cjQyxxStatus.getKssj().toString() + cjQyxxStatus.getJssj().toString());
                        if (!b) {
                            RedisUtil.set(CJ_SB_STATUS_KEY + "JD" + cjQyxxStatus.getNsrsbh() + cjQyxxStatus.getKssj().toString() + cjQyxxStatus.getJssj().toString(), CJ_SB_STATUS_KEY + "JD" + "complete");
                        } else {
                            boolean b1 = RedisUtil.hasKey(CJ_SB_STATUS_KEY + "YF" + cjQyxxStatus.getNsrsbh() + cjQyxxStatus.getKssj().toString() + cjQyxxStatus.getJssj().toString());
                            if (b1) {
                                cjQyxxStatus.setStatus("1");
                                //更新记录
                                cjQyxxStatusService.updateByMultiId(cjQyxxStatus);
                                RedisUtil.del(CJ_SB_STATUS_KEY + "YF" + cjQyxxStatus.getNsrsbh() + cjQyxxStatus.getKssj().toString() + cjQyxxStatus.getJssj().toString(), CJ_SB_STATUS_KEY + "JD" + cjQyxxStatus.getNsrsbh() + cjQyxxStatus.getKssj().toString() + cjQyxxStatus.getJssj().toString());
                            }
                        }
                    }
            return null;
                }
            }));
        }
        for (Future f : futureList) {
            try {
                f.get();
            } catch (Exception e) {
                log.error("同步申报信息数据失败," + e.getMessage());
            }
        }
        return ReturnT.SUCCESS;
    }

    // 采集申报信息数据(按月份)
    @XxlJob(value = "cjYfSbxxJob")
    public ReturnT<String> cjYfSbxxJob(String s) {
        DateUtils utils = DateUtils.getInstance();
        List<Future> futureList = new ArrayList<>();
        LambdaQueryWrapper<CjQyxxStatus> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CjQyxxStatus::getType, "03").eq(CjQyxxStatus::getStatus, "0");
        List<CjQyxxStatus> list = cjQyxxStatusService.list(queryWrapper);
        for (CjQyxxStatus cjQyxxStatus : list) {
            futureList.add(executor.submit(new Callable() {
                boolean flag = true;

                @Override
                public Object call() throws Exception {
                    log.info("开始同步企业{}，开始时间：{}， 结束时间: {}", cjQyxxStatus.getDjxh(),
                            DateUtils.getInstance().format(cjQyxxStatus.getKssj()),
                            DateUtils.getInstance().format(cjQyxxStatus.getJssj()));
                    Date kssj = cjQyxxStatus.getKssj();
                    Date jssj = cjQyxxStatus.getJssj();
                    //判断起止时间超过1年，则全量，否则是增量
                    Calendar kssjCal = Calendar.getInstance();
                    kssjCal.setTime(kssj);
                    Calendar jssjCal = Calendar.getInstance();
                    jssjCal.setTime(jssj);
                    int months = utils.getCountBeforeTwoTime(kssj, jssj);
                    // 全量
                    if (months >= 12) {
                        Date endTime = utils.getCurrentQuarterStartTime(jssj);
                        Calendar endCalendar = Calendar.getInstance();
                        endCalendar.setTime(endTime);
                        while (kssjCal.before(endCalendar)) {
                            // 设置当月最早时间
                            String ssqq = utils.getFirstDayOfMonth(kssjCal);
                            // 设置当月最晚时间
                            String ssqz = utils.getLastDayOfMonth(kssjCal);
                            DjNsrxxPO po = nsrxxService.getByNsrsbhOrShxydm(cjQyxxStatus.getNsrsbh());
                            SbSyncRecord sbSyncRecord = sbSyncRecordService.getByDjxhSsq(po.getDjxh(), ssqq, ssqz);
                            CxnsrysbjlReqVo reqVo = new CxnsrysbjlReqVo();
                            reqVo.setDjxh(po.getDjxh());
                            reqVo.setSkssqq(ssqq);
                            reqVo.setSkssqz(ssqz);
                            LoginSessionVo loginInfo = sfjrApiService.getNsrLoginSessionInfo(po.getNsrsbh());
                            if (sbSyncRecord == null) {
                                sbSyncRecord = new SbSyncRecord();
                                BeanUtils.copyProperties(reqVo, sbSyncRecord);
                                sbSyncRecord.setCreateTime(new Date());
                            }
                            sbSyncRecord.setUpdateTime(new Date());
                            sbSyncRecord.setType("206");
                            sbSyncRecord.setNsrsbh(po.getNsrsbh());
                            try {
                                CxnsrysbjlResponseVo vo = guangdongSfjrApiService.cxnsrysbjl("8970d1011b37488082e338f71898aea3", loginInfo.getZgswjdm(), reqVo);
                                //CxnsrysbjlResponseVo vo = guangdongSfjrApiService.cxnsrysbjl(loginInfo.getSessionId(), loginInfo.getZgswjdm(), reqVo);

                                if (vo == null) {
                                    sbSyncRecord.setStatus("200");
                                } else {
                                    List<CxnsrysbjlResponseVo.CwgzReturnVo> cwgzReturnVO = vo.getCwgzReturnVO();
                                    for (CxnsrysbjlResponseVo.CwgzReturnVo returnVo : cwgzReturnVO) {
                                        String skssqq = returnVo.getSkssqq();
                                        String skssqz = returnVo.getSkssqz();
                                        String sbrq = returnVo.getSbrq();
                                        SbSbxx sbxx = new SbSbxx();
                                        sbxx.setTynsrsbh(po.getTynsrsbh());
                                        sbxx.setSbxxuuid(returnVo.getSbuuid());
                                        sbxx.setSbuuid(returnVo.getSbuuid());
                                        sbxx.setDjxh(po.getDjxh());
                                        sbxx.setPzxh(StringUtils.hasLength(returnVo.getPzxh()) ? returnVo.getPzxh() : "");
                                        sbxx.setYzpzzlDm(StringUtils.hasLength(returnVo.getYzpzzlDm()) ? returnVo.getYzpzzlDm() : "");
                                        String sbfsDm = returnVo.getSbfsDm();
                                        if (StringUtils.hasLength(sbfsDm)) {
                                            sbxx.setSbfsDm(sbfsDm);
                                        } else {
                                            sbxx.setSbfsDm("");
                                        }
                                        if (StringUtils.hasLength(skssqq)) {
                                            sbxx.setNssbrq(utils.strToDate(sbrq, "yyyy-MM-dd"));
                                        }
                                        if (StringUtils.hasLength(skssqq)) {
                                            sbxx.setSkssqq(utils.strToDate(skssqq, "yyyy-MM-dd"));
                                        }
                                        if (StringUtils.hasLength(skssqz)) {
                                            sbxx.setSkssqz(utils.strToDate(skssqz, "yyyy-MM-dd"));
                                        }
                                        sbxx.setYbtse(returnVo.getYbtse().doubleValue());
                                        String zsxmDm = returnVo.getZsxmDm();
                                        if (StringUtils.hasLength(zsxmDm)) {
                                            sbxx.setZsxmDm(returnVo.getZsxmDm());
                                        } else {
                                            sbxx.setZsxmDm("");
                                        }
                                        sbxx.setCreateTime(new Date());
                                        sbxx.setUpdateTime(new Date());
                                        sbSbxxService.saveOrUpdateByMultiId(sbxx);
                                    }
                                    sbSyncRecord.setStatus("100");
                                    sbSyncRecord.setRemark("");
                                }
                            } catch (Exception e) {
                                log.error("调用申报接口错误,", e);
                                sbSyncRecord.setStatus("1");
                                sbSyncRecord.setRemark(e.getMessage() + ", 入参:" + JSON.toJSONString(reqVo));
                                flag = false;
                            }
                            sbSyncRecordService.saveOrUpdate(sbSyncRecord);
                            kssjCal.add(Calendar.MONTH, 1);
                        }
                    } else {
                        // jssjCal.add(Calendar.MONTH, -2);
                        String ssqq = utils.format(kssjCal.getTime());
                        String ssqz = utils.format(jssjCal.getTime());
                        CxnsrysbjlReqVo reqVo = new CxnsrysbjlReqVo();
                        DjNsrxxPO po = nsrxxService.getByNsrsbhOrShxydm(cjQyxxStatus.getNsrsbh());
                        SbSyncRecord sbSyncRecord = sbSyncRecordService.getByDjxhSsq(po.getDjxh(), ssqq, ssqz);
                        reqVo.setDjxh(po.getDjxh());
                        reqVo.setSkssqq(ssqq);
                        reqVo.setSkssqz(ssqz);
                        LoginSessionVo loginInfo = sfjrApiService.getNsrLoginSessionInfo(po.getNsrsbh());
                        if (sbSyncRecord == null) {
                            sbSyncRecord = new SbSyncRecord();
                            BeanUtils.copyProperties(reqVo, sbSyncRecord);
                            sbSyncRecord.setCreateTime(new Date());
                        }
                        sbSyncRecord.setUpdateTime(new Date());
                        sbSyncRecord.setType("206");
                        sbSyncRecord.setNsrsbh(po.getNsrsbh());
                        try {
                            // CxnsrysbjlResponseVo vo = guangdongSfjrApiService.cxnsrysbjl(loginInfo.getSessionId(), loginInfo.getZgswjdm(), reqVo);
                            CxnsrysbjlResponseVo vo = guangdongSfjrApiService.cxnsrysbjl("8970d1011b37488082e338f71898aea3", loginInfo.getZgswjdm(), reqVo);
                            if (vo == null) {
                                sbSyncRecord.setStatus("200");
                            } else {
                                List<CxnsrysbjlResponseVo.CwgzReturnVo> cwgzReturnVO = vo.getCwgzReturnVO();
                                for (CxnsrysbjlResponseVo.CwgzReturnVo returnVo : cwgzReturnVO) {
                                    String skssqq = returnVo.getSkssqq();
                                    String skssqz = returnVo.getSkssqz();
                                    String sbrq = returnVo.getSbrq();
                                    SbSbxx sbxx = new SbSbxx();
                                    sbxx.setTynsrsbh(po.getTynsrsbh());
                                    sbxx.setSbxxuuid(returnVo.getSbuuid());
                                    sbxx.setSbuuid(returnVo.getSbuuid());
                                    sbxx.setDjxh(po.getDjxh());
                                    sbxx.setPzxh(StringUtils.hasLength(returnVo.getPzxh()) ? returnVo.getPzxh() : "");
                                    sbxx.setYzpzzlDm(StringUtils.hasLength(returnVo.getYzpzzlDm()) ? returnVo.getYzpzzlDm() : "");
                                    sbxx.setSbfsDm(StringUtils.hasLength(returnVo.getSbfsDm()) ? returnVo.getSbfsDm() : "");
                                    if (StringUtils.hasLength(skssqq)) {
                                        sbxx.setNssbrq(utils.strToDate(sbrq, "yyyy-MM-dd"));
                                    }
                                    if (StringUtils.hasLength(skssqq)) {
                                        sbxx.setSkssqq(utils.strToDate(skssqq, "yyyy-MM-dd"));
                                    }
                                    if (StringUtils.hasLength(skssqz)) {
                                        sbxx.setSkssqz(utils.strToDate(skssqz, "yyyy-MM-dd"));
                                    }
                                    sbxx.setYbtse(returnVo.getYbtse().doubleValue());
                                    sbxx.setZsxmDm(StringUtils.hasLength(returnVo.getZsxmDm()) ? returnVo.getZsxmDm() : "");
                                    sbxx.setCreateTime(new Date());
                                    sbxx.setUpdateTime(new Date());
                                    sbSbxxService.saveOrUpdateByMultiId(sbxx);
                                }
                                sbSyncRecord.setStatus("100");
                                sbSyncRecord.setRemark("");
                            }
                        } catch (Exception e) {
                            log.error("调用申报接口错误,", e);
                            sbSyncRecord.setStatus("1");
                            sbSyncRecord.setRemark(e.getMessage() + ", 入参:" + JSON.toJSONString(reqVo));
                            flag = false;
                        }
//                        sbSyncRecordService.saveOrUpdate(sbSyncRecord);
                    }
                    if (flag) {
                        boolean b = RedisUtil.hasKey(CJ_SB_STATUS_KEY + "YF" + cjQyxxStatus.getNsrsbh() + cjQyxxStatus.getKssj().toString() + cjQyxxStatus.getJssj().toString());
                        if (!b) {
                            RedisUtil.set(CJ_SB_STATUS_KEY + "YF" + cjQyxxStatus.getNsrsbh() + cjQyxxStatus.getKssj().toString() + cjQyxxStatus.getJssj().toString(), CJ_SB_STATUS_KEY + "YF" + "complete");
                        } else {
                            boolean b1 = RedisUtil.hasKey(CJ_SB_STATUS_KEY + "JD" + cjQyxxStatus.getNsrsbh() + cjQyxxStatus.getKssj().toString() + cjQyxxStatus.getJssj().toString());
                            if (b1) {
                                cjQyxxStatus.setStatus("1");
                                //更新记录
                                cjQyxxStatusService.updateByMultiId(cjQyxxStatus);
                                RedisUtil.del(CJ_SB_STATUS_KEY + "YF" + cjQyxxStatus.getNsrsbh() + cjQyxxStatus.getKssj().toString() + cjQyxxStatus.getJssj().toString(), CJ_SB_STATUS_KEY + "JD" + cjQyxxStatus.getNsrsbh() + cjQyxxStatus.getKssj().toString() + cjQyxxStatus.getJssj().toString());
                            }
                        }
                    }
                    return null;
                }
            }));
        }
        for (Future f : futureList) {
            try {
                f.get();
            } catch (Exception e) {
                log.error("同步申报信息数据失败," + e.getMessage());
            }
        }
        return ReturnT.SUCCESS;
    }

    public static void main(String[] args) {
        DateUtils dateUtils = DateUtils.getInstance();
//        CwbbSyncRecord cwbbSyncRecord = new CwbbSyncRecord();
//        Calendar kssjCal = Calendar.getInstance();
//        String ssqq = dateUtils.format(dateUtils.getCurrentQuarterStartTime(kssjCal.getTime()));
//        String ssqz = dateUtils.format(dateUtils.getCurrentQuarterEndTime(kssjCal.getTime()));
//
//
//        Calendar cal = Calendar.getInstance();
//        int year = cal.get(Calendar.YEAR);
//        int month = cal.get(Calendar.MONTH);
//        cal.set(year, month - 1, 1, 00, 00, 00);
//        Date first = cal.getTime();
//        cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
//        Date last = cal.getTime();
//        System.out.println(first);
//        System.out.println(last);

        Calendar kssjCal = Calendar.getInstance();
        Calendar jssjCal = Calendar.getInstance();
        String date20180101 = dateUtils.getFirstDayOfMonth(2018, 1);
        String date20220208 = dateUtils.getLastDayOfMonth(2022, 5);
        kssjCal.setTime(dateUtils.strToDate(date20180101, "yyyy-MM-dd"));
        jssjCal.setTime(dateUtils.strToDate(date20220208, "yyyy-MM-dd"));
        //jssjCal.add(Calendar.MONTH, -1);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(dateUtils.getCurrentQuarterStartTime(jssjCal.getTime()));
        int month = jssjCal.get(Calendar.MONTH) + 1;
        List<Integer> syncMonth = Arrays.asList(5, 8, 11, 2);
        System.out.println(month);
        jssjCal.add(Calendar.MONTH, -2);
        String ssqq = dateUtils.format(dateUtils.getCurrentQuarterStartTime(jssjCal.getTime()));
        String ssqz = dateUtils.format(dateUtils.getCurrentQuarterEndTime(jssjCal.getTime()));
        System.out.println("开始时间:" + ssqq);
        System.out.println("结束时间:" + ssqz);
        /*int i = 0;
        do {
            System.out.println(++i);
            String ssqq = dateUtils.format(dateUtils.getCurrentQuarterStartTime(kssjCal.getTime()));
            String ssqz = dateUtils.format(dateUtils.getCurrentQuarterEndTime(kssjCal.getTime()));
            System.out.println(ssqq + ", " + ssqz);
            kssjCal.add(Calendar.MONTH, 3);
        } while (kssjCal.before(endCalendar));*/
    }

    // 同步机构任务
    /*@XxlJob(value = "syncJiGouJob")
    public ReturnT<String> syncJiGouJob(String s) {
        List<JhZzjg> list = jhZzjgService.list();
        // LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        for (JhZzjg jhZzjg : list) {
            String parentName = jhZzjg.getUnderOrgname();
            System.out.println("开始:" + parentName);
            if (parentName == null) {
                continue;
            }
            LambdaQueryWrapper<SysDepart> wrapper = Wrappers.lambdaQuery(SysDepart.class);
            if (parentName.contains("广州")) {
                parentName = parentName.substring(2, 6);
                System.out.println(parentName);
                wrapper.eq(SysDepart::getDepartName, parentName);
            } else {
                wrapper.eq(SysDepart::getDepartName, parentName);
            }
            wrapper.eq(SysDepart::getOrgType, 3);
            SysDepart one = sysDepartService.getOne(wrapper);
            if (one == null) {
                continue;
            }
            wrapper.clear();
            System.out.println("企业:" + jhZzjg.getOrgName());
            wrapper.eq(SysDepart::getDepartName, jhZzjg.getOrgName()).eq(SysDepart::getOrgType, 4);
            SysDepart one1 = sysDepartService.getOne(wrapper);
            if (one1 != null) {
                continue;
            }
            SysDepart sysDepart = new SysDepart();
            sysDepart.setDepartName(jhZzjg.getOrgName());
            sysDepart.setCreateTime(new Date());
            sysDepart.setUpdateTime(new Date());
            sysDepart.setJhJigouId(jhZzjg.getId());
            sysDepart.setOrgCategory("2");
            sysDepart.setDepartOrder(0);
            sysDepart.setParentId(one.getId());
            sysDepart.setStatus("0".equals(jhZzjg.getOrgStatus()) ? "1" : "0");
            String id = UUID.randomUUID().toString().replace("-", "");
            sysDepart.setId(id);
            JSONObject formData = new JSONObject();
            formData.put("parentId", one.getId());
            String[] codeArray = (String[]) FillRuleUtil.executeRule(FillRuleConstant.DEPART, formData);
            //update-end--Author:baihailong  Date:20191209 for：部门编码规则生成器做成公用配置
            sysDepart.setOrgCode(codeArray[0]);
            String orgType = codeArray[1];
            sysDepart.setOrgType(String.valueOf(orgType));
            sysDepart.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
            sysDepart.setCreateBy("admin");
            sysDepartService.save(sysDepart);
            System.out.println("结束:" + parentName);
        }
        System.out.println("完成");
        return ReturnT.SUCCESS;
    }*/

    // 同步机构任务
    /*@XxlJob(value = "syncJiGouJob")
    public ReturnT<String> syncJiGouJob(String s) {
        List<JhZzjg> list = jhZzjgService.list();
        // LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        for (JhZzjg jhZzjg : list) {
            String parentName = jhZzjg.getUnderOrgname();
            System.out.println("开始:" + parentName);
            if (parentName == null) {
                continue;
            }
            LambdaQueryWrapper<SysDepart> wrapper = Wrappers.lambdaQuery(SysDepart.class);
            *//*if (parentName.contains("广州")) {
                parentName = parentName.substring(2, 6);
                System.out.println(parentName);
                wrapper.eq(SysDepart::getDepartName, parentName);
            } else {
                wrapper.eq(SysDepart::getDepartName, parentName);
            }*//*
            *//*wrapper.eq(SysDepart::getDepartName, parentName);
            wrapper.eq(SysDepart::getOrgType, 3);
            SysDepart one = sysDepartService.getOne(wrapper);
            if (one == null) {
                continue;
            }
            wrapper.clear();*//*
            SysDepart sysDepart = new SysDepart();
            System.out.println("企业:" + jhZzjg.getOrgName());
            wrapper.eq(SysDepart::getDepartName, jhZzjg.getOrgName()).eq(SysDepart::getOrgType, 4);
            SysDepart one1 = sysDepartService.getOne(wrapper);
            if (one1 != null) {
                continue;
            }
            sysDepart.setDepartName(jhZzjg.getOrgName());
            sysDepart.setCreateTime(new Date());
            sysDepart.setUpdateTime(new Date());
            sysDepart.setJhJigouId(jhZzjg.getId());
            sysDepart.setOrgCategory("2");
            sysDepart.setDepartOrder(0);
            sysDepart.setParentId("5c1a3f0e989f46fbb0592523772f23de");
            sysDepart.setStatus("0".equals(jhZzjg.getOrgStatus()) ? "1" : "0");
            String id = UUID.randomUUID().toString().replace("-", "");
            sysDepart.setId(id);
            JSONObject formData = new JSONObject();
            formData.put("parentId", "5c1a3f0e989f46fbb0592523772f23de");
            String[] codeArray = (String[]) FillRuleUtil.executeRule(FillRuleConstant.DEPART, formData);
            //update-end--Author:baihailong  Date:20191209 for：部门编码规则生成器做成公用配置
            sysDepart.setOrgCode(codeArray[0]);
            String orgType = codeArray[1];
            sysDepart.setOrgType(String.valueOf(orgType));
            sysDepart.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
            sysDepart.setCreateBy("admin");
            sysDepartService.save(sysDepart);
            System.out.println("结束:" + parentName);
        }
        System.out.println("完成");
        return ReturnT.SUCCESS;
    }*/
}
